<?php
namespace GuessFilm\Controller;
use Common\Controller\GuessFilmController;
class LoginController extends GuessFilmController {

    /**
     * 小程序登录
     * @param string
     * @return string
     */
    public function wxlogin(){

        $code =  I('post.code','','htmlspecialchars');
        $appid =  I('post.appid','','htmlspecialchars');
        $secret =  I('post.secret','','htmlspecialchars');

        $info = $_POST['info'];

        if(!$code||!$appid||!$secret||!$info){
            $data = array();

            $this->returnData($data,$msg='参数错误',$code=401);

        }


        $url = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";

        $res = $this->curlget($url);

        if (!$res['openid']) {
            $this->returnData($res,$msg='openid获取失败',$code=100);
        }



        $info= json_decode($info,true);

        $nickName = $info['nickName'];
        $avatarUrl = $info['avatarUrl'];
        $gender = $info['gender'];
        $province = $info['province'];
        $city = $info['city'];
        $country = $info['country'];
        $language = $info['language'];


        if ($res['openid']){

            $where= array(
                'openid'=>$res['openid']
            );

            $user = M('guessfilm_member')->field('guessfilm_member_id,nickname,gender,city,province,country,avatarurl,openid')->where($where)->find(); //判断是否有用户信息

            if ($user){
                $user_id = $user['guessfilm_member_id'];
                $userinfo = array(
                    'nickname'=>$nickName,
                    'gender'=>$gender,
                    'city'=>$city,
                    'province'=>$province,
                    'country'=>$country,
                    'avatarurl'=>$avatarUrl,
                    'language'=>$language,
                    'uinionid' =>$res['unionId']||'',
                    'logintime' =>time(),
                    'openid' =>$res['openid'],
                    'appid'=>$appid,
                    'session_key'=>$res['session_key'],
                );

                M('guessfilm_member')->where($where)->save($userinfo);

            }else{

                $user_id= M('guessfilm_member')->max('guessfilm_member_id')+1;
                $userinfo = array(
                    'guessfilm_member_id'=>$user_id,
                    'nickname'=>$nickName,
                    'gender'=>$gender,
                    'city'=>$city,
                    'province'=>$province,
                    'country'=>$country,
                    'avatarurl'=>$avatarUrl,
                    'language'=>$language,
                    'uinionid' =>$res['unionId']||'',
                    'logintime' =>time(),
                    'openid' =>$res['openid'],
                    'createtime'=>time(),
                    'appid'=>$appid,
                    'sort'=>$user_id,
                    'money'=>'200'
                );

                M('guessfilm_member')->data($userinfo)->add();

            }

        }

        $session_id =   $this->getSessionId();
        //保存数据到session
        // session('userinfo', $userinfo);

        $data = array(
            'openid'=>$res['openid'],
            'session_id' =>$session_id,
            'member_id' =>$user_id
        );

        $this->returnData($data,'成功',200);

    }


}